package fr.emse.tatiana.replayable;

import java.util.List;
import java.util.Vector;

public class SynchronisationEvent {
	
	private Long begin;
	private Long duration = new Long(0);
	private Long now;
	private List<Anchor> anchors;
	private Replayable source;
	private boolean continuepropagation = true;
	private boolean idsynch = false;
	
	public SynchronisationEvent(Replayable source, long time) {
		this(source, time, time, new Long(0));	
		
		
	}
	
	public SynchronisationEvent(Replayable source, long now, long begin, long duration) {
		this(source);
		this.now = now;
		this.begin = begin;
		this.duration = duration;
	}
	
	public SynchronisationEvent(Replayable source) {
		this.source = source;
		anchors = new Vector<Anchor>();
	}
	
	/**
	 * @return the beginselect
	 */
	public Long getBegin() {
		return begin;
	}

	/**
	 * @return the endselect
	 */
	public Long getDuration() {
		return duration;
	}

	/**
	 * @return the now
	 */
	public Long getNow() {
		return now;
	}

	/**
	 * @return the anchor
	 */
	public List<Anchor> getAnchors() {
		return anchors;
	}

	public void addAnchor(Anchor a) {
		anchors.add(a);
	}
	
	/**
	 * Generator of this event. Can be null.
	 * 
	 * @return the source
	 */
	public Replayable getSource() {
		return source;
	}
	
	public boolean isPropagate() {
		return this.continuepropagation;
	}

	public void stopPropagation() {
		this.continuepropagation = false;
	}
	
	@Override
	public String toString() {
		String anchorname = ""+anchors.size()+" events";
		if (anchors.size()>0) {
			anchorname+=" starting with " + anchors.get(0).toString(); 
		}
		if (anchorname.length()>100) anchorname=anchorname.substring(0, 100)+"...";
		return ((now==null)? "": ("Begin: "+ now + " End: "+ (now+duration) + " Duration: "+ duration + "\n"))
				+ anchorname;
	}

	public long getEnd() {
		return now+duration;
	}

	public void setIDsynch(boolean b) {
		idsynch = true;	
	}

	public boolean isIdsynch() {
		return idsynch;
	}
	
}
